package com.example.studyapp

import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
import org.w3c.dom.Text
import java.lang.Exception

class study_choice_question_1 : AppCompatActivity() {
    lateinit var choice_question_text:TextView
    lateinit var choice_question_a:Button
    lateinit var choice_question_b:Button
    lateinit var choice_question_c:Button
    lateinit var choice_question_d:Button
    lateinit var choice_question_prev:Button
    lateinit var choice_question_center:Button
    lateinit var choice_question_next:Button
    lateinit var choice_question_ret:Button
    lateinit var choice_question_title:TextView
    lateinit var choice_question_info:Button
    lateinit var jspis:ArrayList<jstruct_paper_item>
    var i:Int=0
    var should_enabled_prev:Boolean=true
    var should_enabled_next:Boolean=true
    var dont_set=true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_study_choice_question_1)
        choice_question_text=this.findViewById(R.id.choice_question_text)
        choice_question_a=this.findViewById(R.id.choice_question_a)
        choice_question_b=this.findViewById(R.id.choice_question_b)
        choice_question_c=this.findViewById(R.id.choice_question_c)
        choice_question_d=this.findViewById(R.id.choice_question_d)
        choice_question_prev=this.findViewById(R.id.choice_question_prev)
        choice_question_center=this.findViewById(R.id.choice_question_center)
        choice_question_next=this.findViewById(R.id.choice_question_next)
        choice_question_ret=this.findViewById(R.id.choice_question_ret)
        choice_question_title=this.findViewById(R.id.choice_question_title)
        choice_question_info=this.findViewById(R.id.choice_question_info)
        choice_question_info.isEnabled=false
        choice_question_prev.isEnabled=false
        choice_question_prev.setTextColor(Color.TRANSPARENT)
        //choice_question_title.text=define_now_paper_info.papername+"/选择"
        choice_question_title.text="网络体系结构"
        if(define_now_paper_info.papername==""){//表示一道题
            set_view(define_now_paper_item)
            set_listener(define_now_paper_item)
            should_enabled_prev=false
            should_enabled_next=false
            return
        }
        var jtmp:ArrayList<jstruct_paper_item> = arrayListOf<jstruct_paper_item>()
        //根据define_now_paper_info的内容，从服务器读取第一道题
        val jobj= JSONObject(Gson().toJson(jstruct_paper_item_send(item=0,username= define_user_name,paper = define_study_qu_info_title)))
        jspis=jtmp
        object :jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                //处理获取的消息
                try{
                    val jipi:jstruct_paper_item=Gson().fromJson(response.toString(),jstruct_paper_item::class.java)
                    set_view(jipi)
                    jtmp.add(jipi)
                    set_listener(jipi)
                }catch (e:Exception){
                    Toast.makeText(this@study_choice_question_1,"未能处理数据",Toast.LENGTH_LONG)
                        .show()
                }
            }

            override fun myResposeErrorListener(err: VolleyError) {
                Toast.makeText(this@study_choice_question_1,"网络超时",Toast.LENGTH_LONG)
                    .show()
            }
        }
    }

    //JsonArray转换成List
    private fun <T> jsonToList(jsonList: String): List<T> {
        return Gson().fromJson(jsonList, object : TypeToken<ArrayList<T>>() {}.type)
    }

    fun set_view(jipi:jstruct_paper_item){
        if(should_enabled_prev){
            choice_question_prev.isEnabled=false
            choice_question_prev.setTextColor(Color.TRANSPARENT)
        }
        if(should_enabled_next){
            choice_question_next.isEnabled=false
            choice_question_next.setTextColor(Color.TRANSPARENT)
        }
        //choice_question_info.text=(i+1).toString()+"/"+ define_now_paper_info.choice_qu_num.toString()
            //choice_question_text.text=jipi.text.toString()
        choice_question_info.text="2/30"
        choice_question_text.text=jipi.text.toString()
        var mylist=jsonToList<String>(jipi.fanswer.toString())
        choice_question_a.text=mylist[0]
        choice_question_b.text=mylist[1]
        choice_question_c.text=mylist[2]
        choice_question_d.text=mylist[3]
        choice_question_a.background = getDrawable(R.drawable.btn_shape_write)
        choice_question_b.background = getDrawable(R.drawable.btn_shape_write)
        choice_question_c.background = getDrawable(R.drawable.btn_shape_write)
        choice_question_d.background = getDrawable(R.drawable.btn_shape_write)

    }
    //设置界面
    fun set_listener(jipi:jstruct_paper_item){
        var user_cho:Button=choice_question_a
        //如果abcd中有一项被选中，点击看答案则显示正确的选项
        //监听按钮
        choice_question_a.setOnClickListener {
            choice_question_a.background = getDrawable(R.drawable.cho_btn_check)
            choice_question_b.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_c.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_d.background = getDrawable(R.drawable.btn_shape_write)
            user_cho=choice_question_a
            dont_set=false
        }
        choice_question_b.setOnClickListener {
            choice_question_a.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_b.background = getDrawable(R.drawable.cho_btn_check)
            choice_question_c.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_d.background = getDrawable(R.drawable.btn_shape_write)
            user_cho=choice_question_b
            dont_set=false
        }
        choice_question_c.setOnClickListener {
            choice_question_a.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_b.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_c.background = getDrawable(R.drawable.cho_btn_check)
            choice_question_d.background = getDrawable(R.drawable.btn_shape_write)
            user_cho=choice_question_c
            dont_set=false
        }
        choice_question_d.setOnClickListener {
            choice_question_a.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_b.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_c.background = getDrawable(R.drawable.btn_shape_write)
            choice_question_d.background = getDrawable(R.drawable.cho_btn_check)
            user_cho=choice_question_d
            dont_set=false
        }
        choice_question_center.setOnClickListener {
            //将答案按钮的颜色改为绿色
            //Toast.makeText(this,jipi.toString(),Toast.LENGTH_LONG)
              //  .show()
            if(jipi.cho==0){
                choice_question_a.setBackgroundColor(Color.GREEN)
                if(user_cho!=choice_question_a){
                    if(dont_set)
                    user_cho.setBackgroundColor(Color.RED)
                }
            }else if(jipi.cho==1){
                choice_question_b.setBackgroundColor(Color.GREEN)
                if(user_cho!=choice_question_b){
                    if(dont_set)
                    user_cho.setBackgroundColor(Color.RED)
                }
            }else if(jipi.cho==2){
                choice_question_c.setBackgroundColor(Color.GREEN)
                if(user_cho!=choice_question_c){
                    if(dont_set)
                    user_cho.setBackgroundColor(Color.RED)
                }
            }else if(jipi.cho==3){
                choice_question_d.setBackgroundColor(Color.GREEN)
                if(user_cho!=choice_question_d){
                    if(dont_set)
                    user_cho.setBackgroundColor(Color.RED)
                }
            }
            //使能换题键
            if(should_enabled_prev){
                choice_question_prev.isEnabled=true
                choice_question_prev.setTextColor(Color.WHITE)
            }
            if(should_enabled_next){
                choice_question_next.isEnabled=true
                choice_question_next.setTextColor(Color.WHITE)
            }
        }
        choice_question_prev.setOnClickListener {
            //切换上一道题
            if(i<=0){//禁止切换
                //不显示上一个按钮
                    should_enabled_prev=false
                choice_question_prev.isEnabled=false
                choice_question_prev.setTextColor(Color.TRANSPARENT)
                return@setOnClickListener
            }
            should_enabled_prev=false
            should_enabled_next=false
            try{
                i-=1
                set_view(jspis[i])
            }catch (e:Exception){
                //Toast.makeText(this@study_choice_question_1,"未能处理数据",Toast.LENGTH_LONG)
            }
        }
        choice_question_next.setOnClickListener {
            //切换下一道题
            i+=1
            if(jspis.size>i){//说明题目已经存入jspis
                set_view(jspis[i])
            }else if(i>= define_now_paper_info.choice_qu_num){
                //选择题完成，进入填空题界面
                val inte= Intent(this,study_complet_question_1::class.java)
                startActivity(inte)
                this.finish()
            }
            else{//获取新题
                should_enabled_next=true
                should_enabled_prev
                val jobj=JSONObject(Gson().toJson(jstruct_paper_item_send(item=i,username= define_user_name,paper = define_study_qu_info_title)))
                object :jsonInteraction(jobj){
                    override fun myResposeListener(response: JSONObject) {
                        //处理获取的消息
                        try{
                            val jtmp:jstruct_paper_item=Gson().fromJson(response.toString(),jstruct_paper_item::class.java)
                            set_view(jtmp)
                            jspis.add(jtmp)
                        }catch (e:Exception){
                            //Toast.makeText(this@study_choice_question_1,"未能处理数据",Toast.LENGTH_LONG)
                        }
                    }
                }
            }
        }

        choice_question_ret.setOnClickListener {
            this.finish()
        }
    }
}